package com.wdh.testq.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.StringDef;

import com.wdh.testq.R;
import com.wdh.testq.tools.SettingTools;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


/**
 * Created by Gss on 2019/3/25.
 */
public class PermissionDialog extends BaseDialog {

    private TextView tvRouteRemind;
    private TextView tvPermissionList;
    private TextView tvCancel;
    private TextView tvJumpSetting;

    public PermissionDialog(Context mContext) {
        super(mContext);
        setRate(0.9f);
        tvRouteRemind = getRootView().findViewById(R.id.tv_route_remind);
        tvPermissionList = getRootView().findViewById(R.id.tv_permission_list);
        tvCancel = getRootView().findViewById(R.id.tv_cancel);
        tvJumpSetting = getRootView().findViewById(R.id.tv_jump_setting);
    }

    @Override
    protected void initListener() {
        addSetOnClickListener(tvCancel);
        addSetOnClickListener(tvJumpSetting);
    }

    @Override
    protected void initView() {
        String remind = mContext.getResources().getString(R.string.permission_remind);
        tvRouteRemind.setText(String.format(remind, mContext.getResources().getString(R.string.app_name)));
    }

    @Override
    protected boolean getBackPressHide() {
        return false;
    }

    @Override
    protected int getResource() {
        return R.layout.dialog_permission;
    }

    public PermissionDialog setPermissionList(@PermissionType.Type String... pers) {
        StringBuilder sb = new StringBuilder();
        for (String s : pers) {
            sb.append("-\t" + s);
            sb.append("\n");
        }
        sb.delete(sb.length() - 2, sb.length());
        tvPermissionList.setText(sb.toString());
        return this;
    }

    public PermissionDialog setPermissionText(String s) {
        tvPermissionList.setText(s);
        return this;
    }

    @Override
    protected void doOnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                break;
            case R.id.tv_jump_setting:
                SettingTools.getInstance().goSetting(mContext);
                break;
            default:
                break;
        }
        hideDialog();
    }

    public static class PermissionType {
        public static final String CAMERA = "相机";
        public static final String AUDIO = "录音";
        public static final String READ_EXTERNAL_STORAGE = "读取外部存储空间";
        public static final String WRITE_EXTERNAL_STORAGE = "写入外部存储空间";
        public static final String LOCATION = "获取定位";
        public static final String READ_PHONE_STATE = "读取设备标识";
        public static final String GET_ACCOUNTS = "获取手机的账户列表";
        public static final String READ_CONTACT = "读取联系人";
        public static final String READ_SMS = "读取短信";
        public static final String SEND_SMS = "发送短信";
        public static final String CALL_PHONE = "拨打电话";

        @StringDef({CAMERA, AUDIO, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, LOCATION, READ_PHONE_STATE, GET_ACCOUNTS, READ_CONTACT, READ_SMS, SEND_SMS, CALL_PHONE})
        @Retention(RetentionPolicy.SOURCE)
        public @interface Type {
        }
    }
}

